package org.dromara.genealogy.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.MapstructUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.genealogy.domain.vo.GeInfoVo;
import org.dromara.genealogy.domain.bo.GeInfoBo;
import org.dromara.genealogy.service.IGeInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 家谱基本信息
 *
 * @author qianjm
 * @date 2025-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/genealogy/info")
public class GeInfoController extends BaseController {

    private final IGeInfoService geInfoService;

    /**
     * 查询家谱基本信息列表
     */
    @SaCheckPermission("genealogy:info:list")
    @GetMapping("/list")
    public TableDataInfo<GeInfoVo> list(GeInfoBo bo, PageQuery pageQuery) {
        return geInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出家谱基本信息列表
     */
    @SaCheckPermission("genealogy:info:export")
    @Log(title = "家谱基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GeInfoBo bo, HttpServletResponse response) {
        List<GeInfoVo> list = geInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "家谱基本信息", GeInfoVo.class, response);
    }

    /**
     * 获取家谱基本信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("genealogy:info:query")
    @GetMapping("/{id}")
    public R<GeInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(geInfoService.queryById(id));
    }

    /**
     * 新增家谱基本信息
     */
    @SaCheckPermission("genealogy:info:add")
    @Log(title = "家谱基本信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<GeInfoVo> add(@Validated(AddGroup.class) @RequestBody GeInfoBo bo) {
        return R.ok(geInfoService.insertByBo(bo));
    }

    /**
     * 修改家谱基本信息
     */
    @SaCheckPermission("genealogy:info:edit")
    @Log(title = "家谱基本信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<GeInfoVo> edit(@Validated(EditGroup.class) @RequestBody GeInfoBo bo) {
        GeInfoVo vo = geInfoService.updateByBo(bo);
        GeInfoVo flag = geInfoService.updateByBo(bo);
        return flag !=null ? R.ok(vo) : R.fail();
    }

    /**
     * 删除家谱基本信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("genealogy:info:remove")
    @Log(title = "家谱基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(geInfoService.deleteWithValidByIds(List.of(ids), true));
    }
}
